<template>
  <div class="el-main">
    <el-form ref="department" :rules="rules" :model="department" label-width="120px">
      <el-form-item label="科室名称" prop="name" align="left">
        <el-input v-model="department.name"></el-input>
      </el-form-item>
      <el-form-item v-show="false" prop="id" label="id">
        <el-input v-model="department.id"></el-input>
      </el-form-item>

      <el-form-item label="房间号" prop="roomNumber" align="left">
        <el-input v-model="department.roomNumber"></el-input>
      </el-form-item>
   <!--   <el-form-item label="上级部门名称" prop="parentName" align="left">
        <el-cascader
          v-model="department.parentName" :change-on-select="true"
          :options="options"
          @change="handleChange"></el-cascader>
      </el-form-item>-->
<!--      <el-form-item label="状态" prop="status" align="left">
          <el-switch v-model="department.status" :active-value="1" inactive-value="0"></el-switch>
      </el-form-item>-->
      <el-form-item label="是否是重点科室" prop="isKeyDepartment"  align="left">
        <el-switch v-model="department.isKeyDepartment" :active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="department.remark"></el-input>
      </el-form-item>
      <el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('department')">保存</el-button>
          <el-button @click="exits">取消</el-button>
        </el-form-item>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
    import Qs from 'qs';

    export default {
        name: "departmentAdd",
        data() {
            return {
                parentId: 0,
                parentName: [],
                /*options: [],*/
                value: "",
                department: {
                    isKeyDepartment: "0",
                    roomNumber: "",
                    name: "",
                    parentName: [],
                    remark: "",
                    status: 1,
                },
                rules: {
                    name: [
                        {required: true, message: '请输入科室名称', trigger: 'blur'},
                        {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
                    ],
                    remark: [
                        {required: false, message: '请输入活动名称', trigger: 'blur'},
                    ],
                }
            }
        },
        created() {
            let  that=this;
            if(!!that.$route.query.department){
                that.department = that.$route.query.department
            }
            //that.getMenus();
        },
        watch: {
            'department.name': function (val) {

            }
        },
        methods: {
            /*   findByName: function (val) {

                   this.instance.get('/w/departments/findByName', {params: {name: val}}
                   ).then(data => {
                       if (!!data.data) {
                           alert("名称已经存在")
                       }
                   })
               },*/
          /*  getMenus: function () {
                let data = Qs.stringify({parentId: this.parentId})
                this.instance.post('/w/departments/departmentMenus', data
                ).then(data => {
                    let menus = data.data;
                    if (this.parentId === 0) {
                        this.options = menus;
                    }
                })
            },*/
            handleChange(value) {
                console.log(value);
            },
            submitForm(department) {
                let that = this;
                this.$refs[department].validate((valid) => {
                    if (valid) {
                        this.instance.post('/w/departments', this.department
                        ).then(data => {
                            that.$router.push({name: 'department'})
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
          exits(){
            this.$router.push({name: 'department'}) // 只能用 name
          }
        }
    }
</script>

<style scoped>

</style>
